package me.huha.client;

import java.net.InetAddress;
import java.net.UnknownHostException;

import me.huha.client.commons.ElasticSearchDTO;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

/**
 * @Title: ElasticSearchClient.java
 * @Author: Yepx
 * @mail: yepx2013@gmail.com
 * @CreateTime: 2017年9月29日 下午3:08:50
 * @Todo: ElasticSearch链接类
 */
public class ElasticSearchClient {

	private static TransportClient client;

	private ElasticSearchClient() {
	}

	public static synchronized TransportClient getInstance() throws UnknownHostException {

		if (client == null) {

			String clusterName = ElasticSearchDTO.getClusterName();
			String host = ElasticSearchDTO.getHost();
			Integer port = ElasticSearchDTO.getPort();

			Settings settings = Settings.builder().put("cluster.name", clusterName).build();
			client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
		}
		return client;
	}
}
